package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.scity.beens.YijianBeen;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class YjfkActivity extends AppCompatActivity {
    EditText title;
    EditText content;
    Button button;
    Button button1;
    YijianBeen yijianBeen;
    Handler handler=new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    Toast.makeText(YjfkActivity.this,yijianBeen.msg,Toast.LENGTH_LONG).show();
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yjfk);
        title = findViewById(R.id.yijian_title);
        content = findViewById(R.id.yijian_content);
        button = findViewById(R.id.yijian_button);
        button1=findViewById(R.id.yijian_fklb);
        loadyijian();
        intoFklb();
    }
    private void loadyijian() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (Configs.Token.equals("")) {
                    startActivity(new Intent(YjfkActivity.this, LoginActivity.class));
                } else {
                    Map<String, String> map = new HashMap<String, String>();
                    map.put("title", title.getText().toString());
                    map.put("content", content.getText().toString());

                    OkHttpClient okHttpClient = new OkHttpClient();
                    MediaType mediaType = MediaType.parse("application/json");
                    RequestBody requestBody = RequestBody.create(mediaType, new Gson().toJson(map));
                    Request request = new Request.Builder()
                            .url(Configs.Host + APIInterface.YJFK)
                            .addHeader("Authorization", Configs.Token)
                            .post(requestBody)
                            .build();
                    okHttpClient.newCall(request).enqueue(new Callback() {
                        @Override
                        public void onFailure(@NonNull Call call, @NonNull IOException e) {

                        }

                        @Override
                        public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                            yijianBeen = new Gson().fromJson(response.body().string(), YijianBeen.class);
                            handler.sendEmptyMessage(0);
                        }
                    });
                }
            }
        });
    }

    private void intoFklb() {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(YjfkActivity.this,FklbActivity.class));
            }
        });
    }
}